// 页面加载时获取并显示当前设置
document.addEventListener('DOMContentLoaded', function() {
  chrome.storage.sync.get(['secondaryScreenPosition'], function(result) {
    if (result.secondaryScreenPosition) {
      document.getElementById('secondaryScreenLeft').value = result.secondaryScreenPosition.left || '';
      document.getElementById('secondaryScreenTop').value = result.secondaryScreenPosition.top || '';
    }
  });
  
  // 显示欢迎信息
  showStatus("欢迎使用双屏搜索助手！", "success");
});

// 保存设置
document.getElementById('saveSettings').addEventListener('click', function() {
  const left = parseInt(document.getElementById('secondaryScreenLeft').value) || 0;
  const top = parseInt(document.getElementById('secondaryScreenTop').value) || 0;
  
  chrome.storage.sync.set({
    secondaryScreenPosition: {
      left: left,
      top: top
    }
  }, function() {
    if (chrome.runtime.lastError) {
      showStatus("保存失败: " + chrome.runtime.lastError.message, "error");
    } else {
      showStatus("设置已保存成功！", "success");
      
      // 3秒后自动隐藏成功消息
      setTimeout(() => {
        document.getElementById('statusMessage').innerHTML = '';
      }, 3000);
    }
  });
});

// 显示状态消息
function showStatus(message, type) {
  const statusElement = document.getElementById('statusMessage');
  statusElement.textContent = message;
  statusElement.className = 'status ' + type;
}

// 如果在副屏上打开popup，自动检测屏幕位置
window.onload = function() {
  // 获取当前窗口信息
  chrome.windows.getCurrent(function(window) {
    // 如果没有设置过副屏位置，可以建议用户当前窗口位置作为副屏位置
    if (window.left > 0) {
      document.getElementById('secondaryScreenLeft').placeholder = `当前窗口: ${window.left}`;
    }
    
    if (window.top > 0) {
      document.getElementById('secondaryScreenTop').placeholder = `当前窗口: ${window.top}`;
    }
  });
};

// 添加关于新功能的说明
document.addEventListener('DOMContentLoaded', function() {
  // 添加说明文本
  const instructions = document.querySelector('.instructions');
  if (instructions) {
    const newFeatureInfo = document.createElement('div');
    newFeatureInfo.innerHTML = `
      <p><strong>🌟 新功能说明：</strong></p>
      <p>• 右键菜单新增"设置为副屏窗口"选项</p>
      <p>• 可手动指定副屏窗口，确保100%准确性</p>
      <p>• 支持自动定位和手动指定两种模式</p>
      <p>• 搜索功能已更新为巨量百应搜索</p>
      <p><a href="new_usage_guide.html" target="_blank">查看详细使用指南</a></p>
    `;
    newFeatureInfo.style.marginTop = '15px';
    newFeatureInfo.style.padding = '15px';
    newFeatureInfo.style.background = 'linear-gradient(120deg, #e3f2fd 0%, #bbdefb 100%)';
    newFeatureInfo.style.borderRadius = '8px';
    instructions.parentNode.insertBefore(newFeatureInfo, instructions.nextSibling);
  }
});